package com.ztools.pc.service.impl.coupon;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.coupon.CouponExpire;
import com.ztools.common.model.pc.coupon.CouponTemplate;
import com.ztools.common.model.pc.information.WeChatMember;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.coupon.CouponExpireMapper;
import com.ztools.pc.service.coupon.CouponExpireService;
import com.ztools.pc.service.coupon.CouponTemplateService;
import com.ztools.pc.service.information.MemberService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/5/18  14:55
 * @Version 1.0
 */
@Service
public class CouponExpireServiceImpl extends ServiceImpl<CouponExpireMapper, CouponExpire> implements CouponExpireService {


    @Resource
    private CouponTemplateService couponTemplateService;
    @Resource
    private DictService dictService;
    @Resource
    private MemberService memberService;


    @Override
    public Page<CouponExpire> findList(PageUtil<CouponExpire> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<CouponExpire> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<CouponExpire> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<CouponExpire> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }


    @Override
    public List<CouponExpire> findByObject(CouponExpire couponExpire) {
        QueryWrapper<CouponExpire> queryWrapper = new QueryWrapper<>(couponExpire);
        return list(queryWrapper);
    }

    public QueryWrapper<CouponExpire> getQueryWrapper(CouponExpire couponExpire) {
        QueryWrapper<CouponExpire> queryWrapper = new QueryWrapper<>();
        if (couponExpire.getMemberId() != null) {
            queryWrapper.lambda().eq(CouponExpire::getMemberId, couponExpire.getMemberId());
        }
        if (couponExpire.getTemplateId() != null) {
            queryWrapper.lambda().eq(CouponExpire::getTemplateId, couponExpire.getTemplateId());
        }
        if (couponExpire.getCouponType() != null) {
            queryWrapper.lambda().eq(CouponExpire::getCouponType, couponExpire.getCouponType());
        }
        if (couponExpire.getStartDate() != null) {
            queryWrapper.lambda().le(CouponExpire::getExpireTime, couponExpire.getEndDate());
        }
        if (couponExpire.getEndDate() != null) {
            queryWrapper.lambda().gt(CouponExpire::getExpireTime, couponExpire.getStartDate());
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param couponUsedList 对象集合
     */
    @Override
    public void dictionary(List<CouponExpire> couponUsedList) {
        for (CouponExpire couponExpire : couponUsedList) {
            dictionary(couponExpire);
        }
    }

    public void dictionary(CouponExpire couponExpire) {
        if (couponExpire != null) {
            if (couponExpire.getTemplateId() != null) {
                CouponTemplate template = couponTemplateService.getById(couponExpire.getTemplateId());
                couponExpire.setTemplateName(template.getName());
                couponExpire.setAmountMeet(template.getAmountMeet());
                couponExpire.setDiscountAmount(template.getDiscountAmount());
                couponExpire.setDaysLimit(template.getDaysLimit());
                couponExpire.setBackground(template.getBackground());
            }
            if (couponExpire.getCouponType() != null) {
                couponExpire.setCouponTypeName(dictService.findByTypeCode(DictUtil.COUPON_TYPE, couponExpire.getCouponType() + ""));
            }
            if (couponExpire.getMemberId() != null) {
                WeChatMember member = memberService.getById(couponExpire.getMemberId());
                couponExpire.setMemberName(member.getNickName());
            }
        }
    }


}
